import audios from '../service/audios';
import config from "../config";
import { directionEnum } from '../enum/directionEnum';

//  模型抽象类 封装公共模型方法和属性
export default abstract class modelAbstract {
  abstract name: string;
  public direction: directionEnum = directionEnum.bottom;
  public width: number = config.model.width;
  public height: number = config.model.height;
  public abstract ctx: ICanvas;
  constructor(
    public posX: number,
    public posY: number,
  ) {
    this.randomDirection();
  };
  abstract image(): HTMLImageElement;
  protected draw(img?: HTMLImageElement) {
    this.ctx.canvas.drawImage(this.image(), this.posX, this.posY, config.model.width, config.model.height);
  };
  // 随机方向
  randomDirection() {
    this.direction =  Object.keys(directionEnum)[Math.floor(Math.random() * 4)] as directionEnum;
  };
  // 销毁模型
  destroy() {
    this.ctx.removeModels(this);
    this.ctx.renderModel();
  };
  // 爆炸效果
  blast(model: IModel) {
    let me = this;
    audios.blast();
    Array(...Array(8).keys()).reduce((promise, index) => {
      return new Promise((resove => {
        setTimeout(() => {
          let img = new Image();
          img.src = `/src/static/images/blasts/blast${index}.gif`;
          img.onload = function () {
            me.ctx.canvas.drawImage(img, model.posX, model.posY, model.width, model.height);
            resove(promise);
          };
        }, 100);
      }))
    }, Promise.resolve())
  }
}